home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Griffith 0.9.8 / griffith-0.9.8-win32.exe / {app} / lib / loan.py < prev    next >
Text File  |  2008-11-17  |  5KB  |  144 lines

  1. # -*- coding: UTF-8 -*-
  2.  
  3. __revision__ = '$Id: loan.py 1040 2008-11-15 21:13:49Z mikej06 $'
  4.  
  5. # Copyright (c) 2005-2008 Vasco Nunes, Piotr O┼╝arowski
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  20.  
  21. # You may use and distribute this software under the terms of the
  22. # GNU General Public License, version 2 or later
  23.  
  24. import gutils
  25. import gtk
  26. import datetime
  27.  
  28. def loan_movie(self):
  29.     people = self.db.Person.select(order_by='name ASC')
  30.     model = gtk.ListStore(str)
  31.     if len(people)>0:
  32.         for person in people:
  33.             model.append([person.name])
  34.         self.widgets['movie']['loan_to'].set_model(model)
  35.         self.widgets['movie']['loan_to'].set_text_column(0)
  36.         self.widgets['movie']['loan_to'].set_active(0)
  37.         self.widgets['w_loan_to'].show()
  38.     else:
  39.         gutils.info(self, _("No person is defined yet."), self.widgets['window'])
  40.  
  41. def cancel_loan(self):
  42.     self.widgets['w_loan_to'].hide()
  43.  
  44. def commit_loan(self):
  45.     person_name = gutils.on_combo_box_entry_changed(self.widgets['movie']['loan_to'])
  46.     if person_name == '' or person_name is None:
  47.         return False
  48.     self.widgets['w_loan_to'].hide()
  49.  
  50.     person = self.db.Person.get_by(name=person_name)
  51.     if person is None:
  52.         self.debug.show("commit_loan: person doesn't exist")
  53.         return False
  54.     if self._movie_id:
  55.         movie = self.db.Movie.get_by(movie_id=self._movie_id)
  56.         if not movie:
  57.             self.debug.show("commit_loan: wrong movie_id")
  58.             return False
  59.     else:
  60.         self.debug.show("commit_loan: movie not selected")
  61.         return False
  62.  
  63.     # ask if user wants to loan whole collection
  64.     loan_whole_collection = False
  65.     if movie.collection_id>0:
  66.         response = gutils.question(self, msg=_("Do you want to loan whole collection?"), parent=self.widgets['window'])
  67.         if response == gtk.RESPONSE_YES:
  68.             loan_whole_collection = True
  69.         elif response == gtk.RESPONSE_CANCEL:
  70.             return False
  71.     
  72.     loan = self.db.Loan(movie_id=movie.movie_id, person_id=person.person_id)
  73.     if loan_whole_collection:
  74.         loan.collection_id = movie.collection_id
  75.     if movie.volume_id>0:
  76.         loan.volume_id = movie.volume_id
  77.     if loan.set_loaned():
  78.         self.update_statusbar(_("Movie loaned"))
  79.         self.treeview_clicked()
  80.  
  81. def return_loan(self):
  82.     if self._movie_id:
  83.         loan = self.db.Loan.get_by(movie_id=self._movie_id, return_date=None)
  84.         if loan is None:
  85.             movie = self.db.Movie.get_by(movie_id=self._movie_id)
  86.             if movie.collection is not None and movie.collection.loaned:
  87.                 #print len(movie.loans) # FIXME: why it's == 0? (mappers problem?)
  88.                 loan = self.db.Loan.get_by(collection_id=movie.collection.collection_id, return_date=None)
  89.             if movie.volume is not None and movie.volume.loaned:
  90.                 loan = self.db.Loan.get_by(volume_id=movie.volume.volume_id, return_date=None)
  91.         if loan and loan.set_returned():
  92.             self.treeview_clicked()
  93.  
  94. def get_loan_info(db, movie_id, volume_id=None, collection_id=None):
  95.     """Returns current collection/volume/movie loan data"""
  96.     from sqlalchemy import and_, or_
  97.     movie = db.Movie.get_by(movie_id=movie_id)
  98.     if movie is None:
  99.         return False
  100.     
  101.     # fix or add volume/collection data:
  102.     if movie.collection_id is not None:
  103.         collection_id = movie.collection_id
  104.     if movie.volume_id is not None:
  105.         volume_id = movie.volume_id
  106.     
  107.     if collection_id>0 and volume_id>0:
  108.         return db.Loan.get_by(
  109.                 and_(or_(db.Loan.c.collection_id==collection_id,
  110.                         db.Loan.c.volume_id==volume_id,
  111.                         db.Loan.c.movie_id==movie_id),
  112.                     db.Loan.c.return_date==None))
  113.     elif collection_id>0:
  114.         return db.Loan.get_by(
  115.                 and_(or_(db.Loan.c.collection_id==collection_id,
  116.                         db.Loan.c.movie_id==movie_id)),
  117.                     db.Loan.c.return_date==None)
  118.     elif volume_id>0:
  119.         return db.Loan.get_by(and_(or_(db.Loan.c.volume_id==volume_id,
  120.                             db.Loan.c.movie_id==movie_id)),
  121.                         db.Loan.c.return_date==None)
  122.     else:
  123.         return db.Loan.get_by(db.Loan.c.movie_id==movie_id,db.Loan.c.return_date==None)
  124.  
  125. def get_loan_history(db, movie_id, volume_id=None, collection_id=None):
  126.     """Returns collection/volume/movie loan history"""
  127.     from sqlalchemy import and_, or_, not_
  128.     if collection_id>0 and volume_id>0:
  129.         return db.Loan.select_by(and_(or_(db.Loan.c.collection_id==collection_id,
  130.                             db.Loan.c.volume_id==volume_id,
  131.                             db.Loan.c.movie_id==movie_id),
  132.                         not_(db.Loan.c.return_date==None)))
  133.     elif collection_id>0:
  134.         return db.Loan.select_by(and_(or_(db.Loan.c.collection_id==collection_id,
  135.                             db.Loan.c.movie_id==movie_id),
  136.                         not_(db.Loan.c.return_date==None)))
  137.     elif volume_id>0:
  138.         return db.Loan.select_by(and_(or_(db.Loan.c.volume_id==volume_id,
  139.                             db.Loan.c.movie_id==movie_id),
  140.                         not_(db.Loan.c.return_date==None)))
  141.     else:
  142.         return db.Loan.select_by(db.Loan.c.movie_id==movie_id,not_(db.Loan.c.return_date==None))
  143.  
  144.